Thread: Inheritence and operator[]

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Inheritence and operator[]

    Hello, slight problem here.

    I have two classes which look kinda like this:

    Code:
    class A {
    	...
    
    	public:
    		T *operator[] (int);
    
    	...
    };
    
    class B : public A {
    	...
    };
    And a variable:
    Code:
    B x;
    But when I try to do something like x[0] it returns an object of type B, not an object of type T.

    Do I need to do something special to get it to return a T ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What is T?

  3. #3
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by Daved View Post
    What is T?
    An abstract datatype, a class, but I get the same result with any other type.

    EDIT: hmm, very wierd, made a test program with the bare minimum and everything works dandy, must be something else I'm doing.
    Last edited by nempo; 11-06-2007 at 04:27 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That doesn't make sense. Can you post a simple, compilable example that demonstrates the problem? If T is completely unrelated to B, I don't see how x[0] can return a B.

  5. #5
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by Daved View Post
    That doesn't make sense. Can you post a simple, compilable example that demonstrates the problem? If T is completely unrelated to B, I don't see how x[0] can return a B.
    Not sure if I got the psuedo in the original post correct, anyway, here's some code that dosn't compile:

    Code:
    #include <stdio.h>
    
    template <typename T>
    class A {
    	public:
    		char *operator[] (int);
    };
    
    template <typename T>
    char *A<T>::operator[] (int i) {
    	char *test = new char;
    	
    	*test = 'a';
    	
    	return test;
    }
    
    class C {
    	/*...*/
    };
    
    class B : public A<C> {
    };
    
    int main() {
    	B *x;
    	char *y = x[0];
    }
    But if I change x[0] to (*x)[0] , then it compiles, go figure!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That is because x is a pointer and you need to dereference it to call a member function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem is that x is a B*, not a B. When you use operator[] on a pointer, then the compiler will assume it is an array and do a normal array operation on it.

    The reason (*x)[0] works is because you are dereferencing the pointer and therefore you are calling the object's operator[].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create a good operator[]
    By mikel123 in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2008, 10:59 AM
  2. Ambiguous operator[]
    By MacNilly in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2007, 05:06 AM
  3. Operator[] error
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2006, 12:22 PM
  4. Overloaded operator[] question
    By dee.dw in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2006, 12:52 AM
  5. string operator[] giving error
    By FoodDude in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2005, 10:42 AM